package com.lanou.sfu.songforu.music_bd;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.datas.MusicListDatas;

import java.util.ArrayList;
import java.util.List;

public class ManaListAdapter extends BaseAdapter{
	
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	
	public static List<ManaMusic> mList;
	
	
	public ManaListAdapter(Context context,List<ManaMusic> list){
		mContext = context;
		mLayoutInflater = LayoutInflater.from(mContext);
		mList = list;
	}
	  
	
	
	public List<String> getList(){
		List<String> path = new ArrayList<String>();
		for(int i = 0;i<getCount();i++){
			path.add(getMusicPath(i));
		}
		return path;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		Log.d("ManaListAdapter", "mList.size():" + mList.size());
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
  
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ManaListItems listItem = null;
		if(convertView == null){
			listItem = new ManaListItems();
			convertView = mLayoutInflater.inflate(R.layout.mana_music_list_item, null);
			listItem.mMusicArtist = (TextView)convertView.findViewById(R.id.textMusicArtist);
			listItem.mMusicName = (TextView)convertView.findViewById(R.id.textMusicName);
			listItem.mMusicTime = (TextView)convertView.findViewById(R.id.textMusicTime);
			listItem.localMusic = (LinearLayout) convertView.findViewById(R.id.localMusic);
			convertView.setTag(listItem);
		}  
		else{
			listItem = (ManaListItems)convertView.getTag();
		}
		listItem.mMusicName.setText(getMusicName(position));
		listItem.mMusicArtist.setText(getArtist(position));
		listItem.mMusicTime.setText(getMusicTime(position));
		Log.i("localmusic", getMusicName(position) + "");
		listItem.localMusic.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i("cxh", "点击");
				ArrayList<MusicListDatas> lists = new ArrayList<>();
				for (ManaMusic entity : mList) {
					MusicListDatas musicList = new MusicListDatas();
					musicList.setName(entity.getmMusicName());
					musicList.setSinger(entity.getmMusicArtist());
					musicList.setSongId(entity.getmMusicPath());

//                    musicList.setSingerImage(entity.get);
					musicList.setFrom("本地");
					lists.add(musicList);
				}
				MainActivity.play(lists, position);
			}
		});

		return convertView;
	}
	
	// CharSequence与String都能用于定义字符串，但CharSequence的值是可读可写序列，而String的值是只读序列
	
	public CharSequence getMusicTime(int position) {
		// TODO Auto-generated method stub
		return mList.get(position).mMusicTime;
	}

	public CharSequence getArtist(int position) {
		// TODO Auto-generated method stub
		return mList.get(position).mMusicArtist;
	}

	public CharSequence getMusicName(int position) {
		// TODO Auto-generated method stub
		return mList.get(position).mMusicName;
	}
	public String getMusicPath(int position){
		return mList.get(position).mMusicPath;
	}
}
